package bi

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.Point
import android.os.Build
import android.provider.Settings
import android.view.View
import android.view.WindowManager


object  GG {
    /**
     * dp转px
     *
     * @param context
     * @param dp
     * @return
     */
    @Deprecated("")
    fun dip2px(context: Context, dp: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dp * scale + 0.5f).toInt()
    }

    /**
     * px转dp
     *
     * @param context
     * @param pxVal
     * @return
     */
    @Deprecated("")
    fun px2dip(context: Context, pxVal: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (pxVal / scale + 0.5f).toInt()
    }

    /**
     * dp转px
     *
     * @param dp
     * @return
     */
    fun dp2px(dp: Float): Int {
        val scale = Resources.getSystem().displayMetrics.density
        return (dp * scale + 0.5f).toInt()
    }

    /**
     * px转dp
     *
     * @param pxVal
     * @return
     */
    fun px2dp(pxVal: Float): Int {
        val scale = Resources.getSystem().displayMetrics.density
        return (pxVal / scale + 0.5f).toInt()
    }

    /**
     * sp转px
     *
     * @param sp
     * @return
     */
    fun sp2px(sp: Float): Int {
        val scale = Resources.getSystem().displayMetrics.density
        return (sp * scale + 0.5f).toInt()
    }

    /**
     * px转sp
     *
     * @param pxVal
     * @return
     */
    fun px2sp(pxVal: Float): Float {
        return pxVal / Resources.getSystem().displayMetrics.density
    }

    /**
     * sp转px
     *
     * @param context
     * @param sp
     * @return
     */
    @Deprecated("")
    fun sp2px(context: Context, sp: Float): Int {
        val scale = context.resources.displayMetrics.scaledDensity
        return (sp * scale + 0.5f).toInt()
    }

    /**
     * px转sp
     *
     * @param context
     * @param pxVal
     * @return
     */
    @Deprecated("")
    fun px2sp(context: Context, pxVal: Float): Float {
        return pxVal / context.applicationContext.resources.displayMetrics.scaledDensity
    }

    /**
     * 获取屏幕宽度
     *
     * @return
     */
    fun getScreenRealSize(activity: Activity): Point? {
        val outSize = Point()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            activity.windowManager.defaultDisplay.getRealSize(outSize)
        } else {
            outSize.x = getScreenWidth()
            outSize.y = getScreenHeight()
        }
        return outSize
    }

    /**
     * 获取屏幕宽度
     *
     * @return
     */
    fun getScreenWidth(): Int {
        return Resources.getSystem().displayMetrics.widthPixels
    }

    /**
     * 获取屏幕高度
     *
     * @return
     */
    fun getScreenHeight(): Int {
        return Resources.getSystem().displayMetrics.heightPixels
    }

    /**
     * 状态栏透明
     */
    fun transparentStatusBar(activity: Activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = activity.window
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
        }
    }

    /**
     * 用于获取状态栏的高度。
     *
     * @return 返回状态栏高度的像素值。
     */
    @Deprecated("")
    fun getStatusBarHeight(context: Context): Int {
        var statusBarHeight = 0
        if (statusBarHeight == 0) {
            try {
                val c = Class.forName("com.android.internal.R\$dimen")
                val o = c.newInstance()
                val field = c.getField("status_bar_height")
                val x = field[o] as Int
                statusBarHeight = context.applicationContext.resources.getDimensionPixelSize(x)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return statusBarHeight
    }

    fun getStatusBarHeight2(context: Context): Int {
        var result = 0
        val resourceId = context.resources.getIdentifier(
            "status_bar_height", "dimen",
            "android"
        )
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return result
    }

    /**
     * 获取导航栏高度
     *
     * @param context
     * @return
     */
    fun getNavigationBarHeight(context: Context): Int {
        val rid = context.resources.getIdentifier("config_showNavigationBar", "bool", "android")
        return if (rid != 0) {
            val resourceId =
                context.resources.getIdentifier("navigation_bar_height", "dimen", "android")
            context.resources.getDimensionPixelSize(resourceId)
        } else {
            0
        }
    }


    /**
     * 获取当前屏幕截图，包含状态栏
     */
    fun captureWithStatusBar(activity: Activity): Bitmap? {
        val view = activity.window.decorView
        view.isDrawingCacheEnabled = true
        view.buildDrawingCache()
        val bmp = view.drawingCache
        val width = getScreenWidth()
        val height = getScreenHeight()
        val bp = Bitmap.createBitmap(bmp, 0, 0, width, height)
        view.destroyDrawingCache()
        return bp
    }

    /**
     * 获取当前屏幕截图，不包含状态栏
     */
    fun captureWithoutStatusBar(activity: Activity): Bitmap? {
        val view = activity.window.decorView
        view.isDrawingCacheEnabled = true
        view.buildDrawingCache()
        val bmp = view.drawingCache
        val statusBarHeight = getStatusBarHeight(activity)
        val width = getScreenWidth()
        val height = getScreenHeight()
        val bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight)
        view.destroyDrawingCache()
        return bp
    }

    /**
     * 判断手机底部是否支持导航栏显示
     * @param context
     * @return
     */
    fun isNavigationBarShowing(context: Context): Boolean {
        val haveNavigationBar = checkDeviceHasNavigationBar(context)
        if (haveNavigationBar) {
            if (Build.VERSION.SDK_INT >= 17) {
                val brand = Build.BRAND
                val mDeviceInfo: String = if (brand.equals("HUAWEI", ignoreCase = true)) {
                    "navigationbar_is_min"
                } else if (brand.equals("XIAOMI", ignoreCase = true)) {
                    "force_fsg_nav_bar"
                } else if (brand.equals("VIVO", ignoreCase = true)) {
                    "navigation_gesture_on"
                } else if (brand.equals("OPPO", ignoreCase = true)) {
                    "navigation_gesture_on"
                } else {
                    "navigationbar_is_min"
                }
                if (Settings.Global.getInt(context.contentResolver, mDeviceInfo, 0) == 0) {
                    return true
                }
            }
        }
        return false
    }


    private fun checkDeviceHasNavigationBar(context: Context): Boolean {
        var hasNavigationBar = false
        val rs = context.resources
        val id = rs.getIdentifier("config_showNavigationBar", "bool", "android")
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id)
        }
        try {
            val systemPropertiesClass = Class.forName("android.os.SystemProperties")
            val m = systemPropertiesClass.getMethod("get", String::class.java)
            val navBarOverride = m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String
            if ("1" == navBarOverride) {
                hasNavigationBar = false
            } else if ("0" == navBarOverride) {
                hasNavigationBar = true
            }
        } catch (e: Exception) {
        }
        return hasNavigationBar
    }
}